﻿#include "query.h"
#include "ui_query.h"
#include<QFile>
#include<QDebug>
#include<QStandardItemModel>
#include<QStandardItem>
#include<QMessageBox>
#include<QtSql/QSqlDatabase>
#include<QtSql/QSqlQuery>
query::query(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::query)
{
    ui->setupUi(this);
    QFile file("../query.css");
    if(file.open(QFile::ReadOnly)){
        QString str=file.readAll();
        setStyleSheet(str);
    }else{
        qDebug()<<"open query.css fail";
    }
    this->model.setHorizontalHeaderItem(0,new QStandardItem("姓名"));
    this->model.setHorizontalHeaderItem(1,new QStandardItem("学号"));
    this->model.setHorizontalHeaderItem(2,new QStandardItem("性别"));
    this->model.setHorizontalHeaderItem(3,new QStandardItem("年龄"));
    this->model.setHorizontalHeaderItem(4,new QStandardItem("院系"));
    this->model.setHorizontalHeaderItem(5,new QStandardItem("兴趣爱好"));
    this->ui->tableView->setModel(&model);
}

void query::queryInfo(int row, QSqlQuery query)
{
    this->model.setItem(row,0,new QStandardItem(query.value("name").toString()));
    this->model.setItem(row,1,new QStandardItem(query.value("id").toString()));
    this->model.setItem(row,2,new QStandardItem(query.value("sex").toString()));
    this->model.setItem(row,3,new QStandardItem(query.value("age").toString()));
    this->model.setItem(row,4,new QStandardItem(query.value("dept").toString()));
    this->model.setItem(row,5,new QStandardItem(query.value("fun").toString()));
}

void query::clearContent()
{
    this->ui->comboBox->setCurrentIndex(0);
    this->ui->lineEdit->clear();
    this->model.clear();
    this->model.setHorizontalHeaderItem(0,new QStandardItem("姓名"));
    this->model.setHorizontalHeaderItem(1,new QStandardItem("学号"));
    this->model.setHorizontalHeaderItem(2,new QStandardItem("性别"));
    this->model.setHorizontalHeaderItem(3,new QStandardItem("年龄"));
    this->model.setHorizontalHeaderItem(4,new QStandardItem("院系"));
    this->model.setHorizontalHeaderItem(5,new QStandardItem("兴趣爱好"));
    this->ui->tableView->setModel(&model);
}

query::~query()
{
    delete ui;
}

void query::on_pushButton_clicked()
{
    this->model.clear();
    this->model.setHorizontalHeaderItem(0,new QStandardItem("姓名"));
    this->model.setHorizontalHeaderItem(1,new QStandardItem("学号"));
    this->model.setHorizontalHeaderItem(2,new QStandardItem("性别"));
    this->model.setHorizontalHeaderItem(3,new QStandardItem("年龄"));
    this->model.setHorizontalHeaderItem(4,new QStandardItem("院系"));
    this->model.setHorizontalHeaderItem(5,new QStandardItem("兴趣爱好"));
    this->ui->tableView->setModel(&model);
    if(!this->db.connectMysql()){
        QMessageBox::critical(this,"查","数据库连接失败,查询失败!","确定");
        return;
    }
    int ret=this->ui->comboBox->currentIndex();
    QString str=this->ui->lineEdit->text();
    switch (ret) {
    case 0:
    {
        QMessageBox::critical(this,"查","请选择查询方式!","确定");
    }
        break;
    case 1:
    {
        QSqlQuery query(QString("select* from student"));
        query.exec();
        for(int i=0;i<query.size();i++){
            query.next();
            queryInfo(i,query);
        }
    }
        break;
    case 2:
    {
        QSqlQuery query(QString("select* from student where name='%1'").arg(str));
        query.exec();
        for(int i=0;i<query.size();i++){
            query.next();
            queryInfo(i,query);
        }
    }
        break;
    case 3:
    {
        QSqlQuery query(QString("select* from student where id=%1").arg(str));
        query.exec();
        for(int i=0;i<query.size();i++){
            query.next();
            queryInfo(i,query);
        }
    }
        break;
    case 4:
    {
        QSqlQuery query(QString("select* from student where dept='%1'").arg(str));
        query.exec();
        for(int i=0;i<query.size();i++){
            query.next();
            queryInfo(i,query);
        }
    }
        break;
    }
}

void query::on_cancelBtn_clicked()
{
    this->close();
    clearContent();
}
